/**
 * @author mapeng
 * @version 2017-05-04
 */

(function(doc, win) {

	var pageVariables = {
		"currentPage" : 1,
		"pageSize" : 10,
		"handleBack" : "back",

	};

	$(doc).on("pageInit", "#storeManagePage", function(e, pageId, $page, isBack,isForward) {
		if(isBack || isForward) return;

		init4StoreManagePage();
	});
	bindEvent4StoreManagePage();

	//页面进入
	function handlePageLoad(){
		var params = CommonUtil.takeOutParamObject() || {};
		pageVariables.handleBack = params.handleBack || "back";
		params.handleBack = "";
		CommonUtil.injectParamObject(params);
	}

	//返回
	function doBack(){


		if(pageVariables.handleBack == "back"){
			$.router.back();
		}else if(pageVariables.handleBack == "load"){
			var cache = CommonUtil.takeOutParamObject() || {};
			cache.shopId = pageVariables.shopId;
			cache.handleBack = "load";
			CommonUtil.injectParamObject(cache);
			$.router.load("my.html",true);
		}
	}

	function init4StoreManagePage(){

		//处理返回
		handlePageLoad();

		//下拉刷新
		refreshItem({
		    "target": "#storeManagePage .page-body",
		    "callBack": function(reset) {
		        refresh4StoreManageList(reset);
		    }
		});

		//无限加载
		addMoreItem({
		    "target": "#storeManagePage .page-body",
		    "callBack": function(reset) {
		        loadMore4StoreManageList(reset);
		    }
		});

		initStoreManageList();
	}

	function bindEvent4StoreManagePage(){

		//返回
		$(document).on("click","#storeManagePage .header a[name='back']",function(e){
			doBack();			
		});

		//跳转详情
		$(document).on("click","#storeManagePage #storeManageContainer ul li",function(e){
			var $this = $(this);
			var shopId = $this.attr("shopId");
			var channel = $this.attr("channel");

			if(channel == Constants.store.WEIXIN_CHANNEL.weifutong){	//威富通
				var cache = CommonUtil.takeOutParamObject() || {};
				cache.shopId = shopId;
				CommonUtil.injectParamObject(cache);
				$.router.load("store_detail.html",true);
			}else if (channel == Constants.store.WEIXIN_CHANNEL.wangshang || channel == Constants.store.WEIXIN_CHANNEL.none){	//网商、暂未开通微信支付
				var cache = CommonUtil.takeOutParamObject() || {};
				cache.shopId = shopId;
				CommonUtil.injectParamObject(cache);
				$.router.load("store_detail_wangshang.html",true);
			}
			
		});
	}

	//列表：初始化
	function initStoreManageList(){

		pageVariables.currentPage = 1;
		CommonUtil.sendJsonpRequest({
		// storeManageTest.testStoreList({
			"url" : App.serverUrl+"/app/shop/getShopList.do",
			"params" : {"currentPage" : pageVariables.currentPage,"pageSize" : pageVariables.pageSize},
			"success" : function(result){
				if(result.success.toString() == "true"){//success
					if(result.resultObject.pageData.length > 0){
						CommonUtil.render($("#storeManagePage .page-body #storeManageContainer"), "storeManageTPL",{"data":result.resultObject.pageData,"constants" : Constants},false);
					}else{
						CommonUtil.render($("#storeManagePage .page-body #storeManageContainer"), "storeManage4NoDataTPL",{},false);
					}

					//控制分页
					if(result.resultObject.nextPage == "0"){
						disableAddMoreItem("#storeManagePage .page-body");
					}else{
						pageVariables.currentPage = pageVariables.currentPage + 1;
						initAddMoreItem("#storeManagePage .page-body");
					}
					
				}else{//business error
					DialogUtil.dialogWarn(result.msg);
				}
			},
			"error" : function(){//not 200
				DialogUtil.dialogWarn("请求失败，请稍候重试");
			},
			"useDefaultLoading" : false
		});
	}

	//列表：刷新
	function refresh4StoreManageList(reset){

		pageVariables.currentPage = 1;
		CommonUtil.sendJsonpRequest({
		// storeManageTest.testStoreList({
			"url" : App.serverUrl+"/app/shop/getShopList.do",
			"params" : {"currentPage" : pageVariables.currentPage,"pageSize" : pageVariables.pageSize},
			"success" : function(result){
				reset();
				if(result.success.toString() == "true"){//success
					if(result.resultObject.pageData.length > 0){
						CommonUtil.render($("#storeManagePage .page-body #storeManageContainer"), "storeManageTPL",{"data":result.resultObject.pageData,"constants" : Constants},false);
					}else{
						CommonUtil.render($("#storeManagePage .page-body #storeManageContainer"), "storeManage4NoDataTPL",{},false);
					}
					
					//控制分页
					if(result.resultObject.nextPage == "0"){
						disableAddMoreItem("#storeManagePage .page-body");
					}else{
						pageVariables.currentPage = pageVariables.currentPage + 1;
						initAddMoreItem("#storeManagePage .page-body");
					}
				}else{//business error
					DialogUtil.dialogWarn(result.msg);
				}
			},
			"error" : function(){//not 200
				reset();
				DialogUtil.dialogWarn("请求失败，请稍候重试");
			}
		});
	}

	//列表：无限加载
	function loadMore4StoreManageList(reset){
		CommonUtil.sendJsonpRequest({
		// storeManageTest.testStoreList({
			"url" : App.serverUrl+"/app/shop/getShopList.do",
			"params" : {"currentPage" : pageVariables.currentPage,"pageSize" : pageVariables.pageSize},
			"success" : function(result){
				if(result.success.toString() == "true"){//success
					if(result.resultObject.pageData.length > 0){
						CommonUtil.render($("#storeManagePage .page-body #storeManageContainer"), "storeManageTPL",{"data":result.resultObject.pageData,"constants" : Constants},true);
					}
					
					//控制分页
					if(result.resultObject.nextPage == "0"){
						reset(true);
					}else{
						reset();
						pageVariables.currentPage = pageVariables.currentPage + 1;
					}
				}else{//business error
					reset();
					DialogUtil.dialogWarn(result.msg);
				}
			},
			"error" : function(){//not 200
				reset();
				DialogUtil.dialogWarn("请求失败，请稍候重试");
			}
		});
	}

	var storeManageTest = {
		testStoreList : function(options){
			var data = [
				{"shopId" : "1" , "mainShopName" : "门店1","serviceVersionType" : "0","isOnline" : "F","proUpgradeStatus" : "0"},
				{"shopId" : "2" , "mainShopName" : "门店2","serviceVersionType" : "1","isOnline" : "T","proUpgradeStatus" : "1"},
				{"shopId" : "3" , "mainShopName" : "门店3","serviceVersionType" : "2","isOnline" : "T","proUpgradeStatus" : "2"},
				{"shopId" : "4" , "mainShopName" : "门店4","serviceVersionType" : "3","isOnline" : "T","proUpgradeStatus" : "3"},
				{"shopId" : "5" , "mainShopName" : "门店5","serviceVersionType" : "0","isOnline" : "F","proUpgradeStatus" : "0"},
				{"shopId" : "6" , "mainShopName" : "门店6","serviceVersionType" : "1","isOnline" : "T","proUpgradeStatus" : "1"},
				{"shopId" : "7" , "mainShopName" : "门店7","serviceVersionType" : "2","isOnline" : "T","proUpgradeStatus" : "2"},
				{"shopId" : "8" , "mainShopName" : "门店8","serviceVersionType" : "3","isOnline" : "T","proUpgradeStatus" : "3"},
				{"shopId" : "9" , "mainShopName" : "门店9","serviceVersionType" : "0","isOnline" : "F","proUpgradeStatus" : "0"},
				{"shopId" : "10" , "mainShopName" : "门店10","serviceVersionType" : "1","isOnline" : "T","proUpgradeStatus" : "1"},
				{"shopId" : "11" , "mainShopName" : "门店11","serviceVersionType" : "2","isOnline" : "T","proUpgradeStatus" : "2"},
				{"shopId" : "12" , "mainShopName" : "门店12","serviceVersionType" : "3","isOnline" : "T","proUpgradeStatus" : "3"}
			];

			options.success({
				"success" : true,
				"code" : "1",
				"msg" : "",
				"resultObject" : {
					"pageData" : data.slice((options.params.currentPage-1)*options.params.pageSize,(options.params.currentPage-1)*options.params.pageSize+options.params.pageSize),
					"nextPage" : (data.length > (options.params.currentPage * options.params.pageSize) )? (options.params.currentPage + 1) : 0
				}
				
			});
		}
	};

})(document, window);